class MovableTypeService < XmlRpcService
  web_service_api MovableTypeApi
  before_invocation :authenticate, :except => [:supportedMethods, :supportedTextFilters]

  def getRecentPostTitles(blogid, username, password, numberOfPosts)
    Media.find(:all, :order => 'id DESC',:limit=>numberOfPosts).collect { |c| MovableTypeStructs::Post.new(:dateCreated=>c.publicationdate,:userid => c.user_id, :postid => c.id,:title=>c.title) }
  end

  def getCategoryList(blogid, username, password)
    Category.find(:all, :order => 'id ASC').collect { |c| MovableTypeStructs::Category.new(:categoryId => c.id, :categoryName => c.name) }
  end

  def getPostCategories(postid, username, password)
    article = @user.medias.find(postid)
    [MovableTypeStructs::Category.new(:categoryId =>article.category.id,:categoryName=>article.category.name)]
    #Category.find(:all).collect { |c| MovableTypeStructs::Category.new(:categoryId => c.id, :categoryName => c.name) }
  end

  def setPostCategories(postid, username, password, categories)
    article = @user.medias.find(postid)
    article.category_id = categories.first.collect { |c| c.categoryId }
    article.save!
    true
  end

  def supportedMethods
    MetaWeblogService.public_instance_methods(false).collect { |m| "metaWeblog.{#m}" } \
    + MovableTypeService.public_instance_methods(false).collect { |m| "mt.{#m}" }
  end

  def supportedTextFilters
    FilteredColumn.filters.collect { |(key, filter)| MovableTypeStructs::TextFilter.new(:key => key, :label => filter.filter_name) }
  end

  # def getTrackbackPings,
  #  ...
  # end

  def publishPost(postid, username, password)
    article = @user.articles.find(postid)
    article. hmmm now what?
    true
  end

end
